package skeleton
{
	import flash.events.AccelerometerEvent;
	import flash.sensors.Accelerometer;

	public class AccelerometerHelper
	{
		private var _accelerometer:Accelerometer;
		private var _actived:Boolean;
		private var _enabled:Boolean;
		
		private var _cbUpdate:Function;
		
		public function AccelerometerHelper(cbUpdate:Function)
		{
			if(Accelerometer.isSupported)
			{
				_accelerometer = new Accelerometer();
				if(!_accelerometer.muted)
				{
					_actived = true;
					trace("accelerometer is actived");

					_accelerometer.addEventListener(AccelerometerEvent.UPDATE, onUpdate);
				}
			}
			
			_cbUpdate = cbUpdate;

			enable();
		}
		
		public function isActived():Boolean
		{
			return _actived;
		}
		
		public function enable():void
		{
			_enabled = true;
		}
		
		public function disable():void
		{
			_enabled = false;
		}
		
		private function onUpdate(e:AccelerometerEvent):void
		{
			if(_enabled)
			{
				if(_cbUpdate != null)
				{
					_cbUpdate(e.timestamp, e.accelerationX, e.accelerationY, e.accelerationZ);
				}
			}
		}
		
	}
}